#ifndef _DSWVIDEOSESSION_LOCAL_CLIENT_H
#define _DSWVIDEOSESSION_LOCAL_CLIENT_H

#include <libtcpcombase/tcpcombase.h>
#include "dswvideosession.h"
#include <libonvif_client/dswonvifrecoder.h>

class CDswvideosession_local_client : public CTcpcombase
{
public:
	CDswvideosession_local_client();
	virtual ~CDswvideosession_local_client();
public:
	CDswvideosession_local_client(LPST_ENV_T pEnv, struct bufferevent * pBev, int nHeadSize, char* pHost, int nPort,
		char* pPUAID, CB_SESSION_NOTIFY pNotifyFunc, void* pCaller, int nExChId);
	static CDswvideosession_local_client* createNew(LPST_ENV_T pEnv, char* pHost, int nPort, char* pPUAID, CB_SESSION_NOTIFY pNotifyFunc, void* pCaller,
		int nExChId);

	static bool cbDealReportIDReq(void* pMsgReq, const char* pMsgResp, void* pCaller){return ((CDswvideosession_local_client*)pCaller)->DealReportIDReq((char*)pMsgReq, pMsgResp);};

	void GetFrameFromMem(LPST_VIDEO_FRAME_T pFrame);
	void SetDiskStatus(ENM_HD_STATUS nStatus);
	ENM_HD_STATUS GetDiskStatus(){return m_nHdStatus;};
	void ForceSave();
	int GetExtChId(){return m_nExChId;};
	char* GetHost(){return m_sHost;};
	int GetPort(){return m_nPort;};
private:
	virtual int MakePayloadLength(char* pData, int nLen);
	virtual bool ProcessMessage(char* pMsg, int nLen, struct evbuffer *outbuf);
	virtual void OnConnect(struct bufferevent * bev, short events);
	virtual void OnError(EN_ERR_TYPE nType, struct bufferevent * bev, short events);

	void SendMsg(char* pData, int nLen);
	void Make_ReportIDResp(CMarkup &xml);
	void Make_ACKReq(CMarkup &xml);
	bool DealReportIDReq(void* pMsgReq, const char* pMsgResp);
	bool ProcessVideoMsg(char* pMsg, int nLen);

	void StartRecoder(int nWidth, int nHeight, int nFps);
	void StopRecoder();

	static void cbCheckDiskTask(int fd, short events, void* ctx){((CDswvideosession_local_client*)ctx)->CheckDiskTask(fd, events);};
	void CheckDiskTask(int fd, short events);

private:
	int m_nExChId;
	char m_sPUAID[64], m_sPUCID[64];
	CB_SESSION_NOTIFY m_pNotifyFunc;
	void* m_pParent;

	ST_VIDEO_MEM_T m_video_mem;
	pthread_mutex_t m_nRecoderLock;
	CDswonvifrecoder* m_pRecoder;
	ENM_HD_STATUS m_nHdStatus;

	time_t m_nStartTm;
	char m_sHost[32];
	int m_nPort;

	timer_param_t m_nCheckDiskTask;
};

#endif

